package bkone;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class addping
 */
@WebServlet("/addping")
public class addping extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addping() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("prompt: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		String nameap = (String)request.getSession().getAttribute("namet");
		System.out.println("addping:" + nameap);
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn;
		try {
			conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			Statement stmt = conn.createStatement();
			String sqlg = "select id from user_zc where name = '" + nameap + "'";
			ResultSet rsg = stmt.executeQuery(sqlg);
			System.out.println("addping:" + rsg);
			String setidg = "";
			while (rsg.next()) {
				String getidg = rsg.getString("id");
			    System.out.println("addping:" + getidg);
			    setidg = getidg;
			}
			System.out.println("addping:" + setidg);
			SimpleDateFormat sdfFull3 = new SimpleDateFormat("yyyy-MM-dd ");
			SimpleDateFormat sdfFull4 = new SimpleDateFormat(":mm:ss");
			Calendar cld = Calendar.getInstance();
			Date currentDate = new Date();
			System.out.println("完整的(前)：" + sdfFull3.format(currentDate));
			System.out.println("完整的(后)：" + sdfFull4.format(currentDate));
			System.out.println("当前天,时间：" + cld.get(Calendar.HOUR_OF_DAY));
			String now_time1 = sdfFull3.format(currentDate) + cld.get(Calendar.HOUR_OF_DAY) + sdfFull4.format(currentDate);
			System.out.println("当前时间：" + now_time1);
			String contentg = request.getParameter("ping_content");
	        System.out.println(contentg);
	        String blogdeid = (String)request.getSession().getAttribute("bokedeid");  
	        System.out.println(blogdeid);
	        String reviewluru = "insert into review (reviewer,revew_time,review_content,bei_id,ping_id) values(?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(reviewluru);
			pstmt.setString(1, nameap);
			pstmt.setString(2, now_time1);
			pstmt.setString(3, contentg);
			pstmt.setString(4, blogdeid);
			pstmt.setString(5, setidg);
  			pstmt.executeUpdate();
			pstmt.close();
			rsg.close();
		    stmt.close();
		    conn.close();
		} catch (SQLException e) {
		    e.printStackTrace();
		}
	}

}
